import pickle

from tools.page import page_get
from tools.处理excel import createAddress
from tools.滑块验证码 import 判断是否存在滑块验证码


def 收货地址获取(date):
    page = page_get()
    with open(f'D:\\快递赔付\\{date}\\data.pkl', 'rb') as file:
        data_list = pickle.load(file)
    page('text=订单').click()
    page('text=打单发货（极速版）').click()
    page.wait(5)
    iframe = page.get_frame('#iframe-gyl_print_service', timeout=60)
    iframe.wait.ele_loaded('text=已发货', timeout=60)
    iframe('text=已发货').click()
    iframe.wait(5)
    for data in data_list:
        if len(data) >= 22:
            continue
        iframe('#Print_Sent_trackNoList').input(data[6], clear=True)
        iframe('text=搜 索').click()
        while True:
            订单号 = iframe('@_nk=cSfn42').text
            if 订单号 in str(data[0]):
                iframe('@_nk=LBh5a1').ele('@_nk=ITyt31').click(by_js=True)
                iframe.wait(3)
                break
        if iframe('text=搜 索').states.is_covered:
            if iframe.s_eles('.anticon anticon-close ant-modal-close-icon'):
                iframe('.anticon anticon-close ant-modal-close-icon').click()
            else:
                msg = 判断是否存在滑块验证码(iframe)
                iframe.wait(3)
                if msg == '滑块验证码超过3次验证失败':
                    page.refresh()
                    raise Exception(msg)
        if page.tabs_count > 1:
            page.close_tabs(others=True)
        收货地址 = iframe('@_nk=LBh581').text
        data.append(收货地址)
        iframe.wait(1)
        with open(f'D:\\快递赔付\\{date}\\data.pkl', 'wb') as file:
            pickle.dump(data_list, file)
    create_list = []
    for _ in data_list:
        create_list.append(_[:22])
    createAddress(f'D:\\快递赔付\\{date}\\快递赔付.xlsx', create_list)
